import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import window from '@ohos.window';
import Logger from '../common/utils/Logger';
import connection from '@ohos.net.connection';

const TAG: string = 'EntryAbility'

export default class EntryAbility extends UIAbility {
    currentNet: connection.NetConnection | undefined = undefined

    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
        Logger.info(TAG, 'Ability onCreate');
    }

    onDestroy(): void {
        Logger.info(TAG, 'Ability onDestroy');
    }

    onWindowStageCreate(windowStage: window.WindowStage): void {
        // Main window is created, set main page for this ability
        Logger.info(TAG, 'Ability onWindowStageCreate');

        windowStage.loadContent('pages/Index', (err, data) => {
            if (err.code) {
                Logger.error(TAG, `Failed to load the content. Cause:, ${JSON.stringify(err)}`);
                return;
            }
            Logger.info(TAG, `Succeeded in loading the content. Data: ${JSON.stringify(data)}`);
        });
    }

    onWindowStageDestroy(): void {
        // Main window is destroyed, release UI related resources
        Logger.info(TAG, 'Ability onWindowStageDestroy');
    }

    onForeground(): void {
        // 应用在前台时将isBackground改为false，用于控制是否弹窗
        AppStorage.setOrCreate('isBackground', false)
        // Ability has brought to foreground
        Logger.info(TAG, 'Ability onForeground');
    }

    onBackground(): void {
        // 应用退到后台将isBackground改为true，用于控制是否弹窗
        AppStorage.setOrCreate('isBackground', true)
        // Ability has back to background
        Logger.info(TAG, 'Ability onBackground');
    }
}
